package com.hackintosh.dating;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * @description：
 * @author：twislyn
 * @date：Created in 2021/06/06
 * @modified By：
 * @version: 1.0
 */
public class DatingGameWin extends JFrame implements ActionListener {
    JPanel centerPanel, eastPanel;
    JRadioButton girl1, girl2, girl3, girl4;
    JButton button1, button2;
    URL fileNameUrl;
    JLabel g;
    John john;
    GirlStack girls;
    public DatingGameWin() {
        super("利用备忘录模式设计相亲游戏");
        john = new John();
        girls = new GirlStack();
        this.setBounds(0, 0, 900, 380);
        this.setResizable(false);
        fileNameUrl = this.getClass().getClassLoader().getResource("four.jpg");
        g = new JLabel(new ImageIcon(fileNameUrl), JLabel.CENTER);
        centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(1, 4));
        centerPanel.setBorder(BorderFactory.createTitledBorder("四大美女如下："));
        centerPanel.add(g);
        this.add("Center", centerPanel);
        eastPanel = new JPanel();
        eastPanel.setLayout(new GridLayout(1, 1));
        eastPanel.setBorder(BorderFactory.createTitledBorder("您选择的爱人是："));
        this.add("East", eastPanel);
        JPanel SouthJP = new JPanel();
        JLabel info = new JLabel("四大美女有“沉鱼落雁之容、闭月羞花之貌”，您选择谁？");
        girl1 = new JRadioButton("西施", true);
        girl2 = new JRadioButton("貂蝉");
        girl3 = new JRadioButton("王昭君");
        girl4 = new JRadioButton("杨玉环");
        button1 = new JButton("确定");
        button2 = new JButton("返回");
        ButtonGroup group = new ButtonGroup();
        group.add(girl1);
        group.add(girl2);
        group.add(girl3);
        group.add(girl4);
        SouthJP.add(info);
        SouthJP.add(girl1);
        SouthJP.add(girl2);
        SouthJP.add(girl3);
        SouthJP.add(girl4);
        SouthJP.add(button1);
        SouthJP.add(button2);
        button1.addActionListener(this);
        button2.addActionListener(this);
        this.add("South", SouthJP);
        showPicture("white");
        john.setWifeName("white");
        girls.push(john.createMemento());    //保存状态
    }
    //显示图片
    void showPicture(String name) {
        eastPanel.removeAll(); //清除面板内容
        eastPanel.repaint(); //刷新屏幕
        john.setWifeName(name);
        fileNameUrl = this.getClass().getClassLoader().getResource( name + ".jpg");
        g = new JLabel(new ImageIcon(fileNameUrl), JLabel.CENTER);
        eastPanel.add(g);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        boolean ok = false;
        if (e.getSource() == button1) {
            ok = girls.push(john.createMemento());    //保存状态
            if (ok && girl1.isSelected()) {
                showPicture("xishi");
            } else if (ok && girl2.isSelected()) {
                showPicture("diaochan");
            } else if (ok && girl3.isSelected()) {
                showPicture("zhaojun");
            } else if (ok && girl4.isSelected()) {
                showPicture("yuhuan");
            }
        } else if (e.getSource() == button2) {
            john.restoreMemento(girls.pop()); //恢复状态
            showPicture(john.getWifeName());
        }
    }
}
